package com.mida.app.knw.controller;


import com.mida.app.knw.service.KnwCompositeInstanceService;
import com.mida.dms.knw.api.KnwCompositeInstanceApi;
import com.mida.dms.knw.vo.KnwCompositeInstanceVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName: KnwCompositeInstanceController
 * @Description: 复合实例controller
 * @Author: wang xiao le
 * @Date: 2021/6/29 13:41
 **/
@RestController
@RequestMapping("/knwCompositeInstance")
@Slf4j
@CrossOrigin
public class KnwCompositeInstanceController {


    @Autowired
    private KnwCompositeInstanceApi knwCompositeInstanceApi;

    @Autowired
    private KnwCompositeInstanceService knwCompositeInstanceService;

    /**
     * 添加复合实例
     * @param knwCompositeInstanceVo
     * @return
     */
    @PostMapping(value = "/addKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult addKnwCompositeInstance(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {

        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.add(knwCompositeInstanceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("添加模版基本信息失败");
            return result;
        }
        return result;
    }

    /**
     * 修改复合实例数据
     *
     * @param knwCompositeInstanceVo
     * @return
     */
    @PostMapping(value = "/updateKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult updateKnwCompositeInstance(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.update(knwCompositeInstanceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("修改模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 删除复合实例数据
     *
     * @param knwCompositeInstanceVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/deleteKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult deleteKnwCompositeInstance(@RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.delete(knwCompositeInstanceVo);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("删除模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 查询复合实例分页列表处理
     *
     * @param knwCompositeInstanceVo 对象接收前端传入的json
     * @return
     */
    @PostMapping(value = "/getKnwCompositeInstancePage",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ListResult<PageResultVo<KnwCompositeInstanceVo>> getKnwCompositeInstancePage(
            @RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        return knwCompositeInstanceService.getKnwCompositeInstancePage(knwCompositeInstanceVo);
    }

    /**
     * 排序复合实例
     *
     * @param
     * @return
     */
    @PostMapping(value = "/updateSort",consumes = {"application/json"})
    public ActionResult updateSort(@RequestBody List<KnwCompositeInstanceVo> knwCompositeInstanceVoList) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceApi.updateSort(knwCompositeInstanceVoList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("排序失败");
            return result;
        }
        return result;
    }

    /**
     * 启用与禁用复合实例数据
     *
     * @param knwCompositeInstanceVo
     * @return
     */
    @PostMapping(value = "/enableKnwCompositeInstance",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ActionResult enableKnwCompositeInstance(@RequestBody KnwCompositeInstanceVo knwCompositeInstanceVo) {
        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceApi.enableKnwCompositeInstance(knwCompositeInstanceVo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("修改模版数据失败");
            return result;
        }
        return result;
    }

    /**
     * 批量上传
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadFile")
    @ResponseBody
    public ActionResult uploadFile(@RequestPart("file") MultipartFile file) {

        ActionResult result = new ActionResult();
        try {
            result = knwCompositeInstanceService.uploadFile(file);
        } catch (Exception e) {
            log.error("常用语批量上传处理发生异常:"+ e.getMessage(), e);
            result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            result.setMessage("常用语批量上传处理失败");
            return result;
        }
        return result;
    }

    /**
     * 下载模板并写入当前登录用户HISID
     * @param res
     */
    @PostMapping("/downloadFile")
    @ResponseBody
    public ActionResult downloadFile(HttpServletResponse res) {
        return knwCompositeInstanceService.downloadFile(res);
    }
}
